﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XiDainChangAn.OA.Common;
using XiDainChangAn.OA.IBLL;
using XiDainChangAn.OA.WebApp.Models;

namespace XiDainChangAn.OA.WebApp.Controllers
{
    [MyLoginCheckFilterAttribute(IsCheck =false)]
    public class UserLoginController : Controller
    {
        public IUserInfoService UserInfoService { get; set; }

        // GET: UserLogin
        public ActionResult Index()
        {
            return View();
        }

        #region 验证码
        public ActionResult ShowVCode()
        {
            string vCode = ValidateCode.CreateRandomCode(4);

            //将验证码放到session中，以便验证信息
            Session["vcode"] = vCode;

            var img = ValidateCode.DrawImage(vCode, background: Color.White);
            return File(img, @"image/gif");
        }
        #endregion

        #region 登陆表单处理
        public ActionResult ProcessLogin()
        {
            //第一步  处理验证码
            string strvcode = Request["vcode"];
            string sessionCode = Session["vcode"].ToString();
            Session.Remove("vcode");
            if (sessionCode.ToLower() != strvcode.ToLower())
            {
                //验证码错误
                return Content("请正确填写验证码");
            }
            //第二步  处理用户名和密码
            string userName = Request["username"];
            string password = Request["password"];

            short delNormal = (short)XiDianChangAn.OA.Model.Enum.DelFlagEnum.Normal;
            var userInfo= UserInfoService.GetEntitys(u => u.UName == userName && u.PassWord == password && u.DelFlag == delNormal).FirstOrDefault();

            if (userInfo == null)//没有查询到数据
            {
                return Content("用户名密码错误！！");
            }

            Session["user"] = userInfo;

            //第三步  验证通过，跳转到首页
            return Content("ok");
        }
        #endregion
    }
}